#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk

from modules.pad_editor import Pad_editor
from common.pad_fun import *

class Pad_notebook(gtk.Notebook):
    def __init__(self):
        gtk.Notebook.__init__(self)

        self.__editor_list = [Pad_editor(),]

        self.append_page_all()

        #self.connect('page-removed', self.on_page_removed)

    #def on_page_removed(self, notebook, child, page_num):
    #    if self.get_n_pages() == 0:
    #        gtk.main_quit()

    def append_page_all(self):
        """"""
        map(lambda(editor):self.append_page(editor, editor.get_label()), self.__editor_list)

    def append_page_and_editor(self, editor):
        self.__editor_list.append(editor)
        self.append_page(editor, editor.get_label())
        self.show_all()

    def find_pos_with_abs_filename(self, abs_filename):
        """
        根据文件的绝对路径返回editor在editor_list中的位置
        @param abs_filename:文件的绝对路径
        @type abs_filename:string
        @return:如果在列表中返回其位置 否则返回None
        @rtype:int
        """
        for i, editor in enumerate(self.__editor_list):
            if editor.get_abs_filename() == abs_filename:
                return i
        return None

    def get_editor_list(self):
        return self.__editor_list

    def get_current_editor(self):
        """
        返回当前editor
        @return:返回当前editor
        @rtype:Pad_editor
        """
        return self.__editor_list[self.get_current_page()]

    def remove_page_and_editor(self, editor):
        """
        删除editor_info、notebook中此editor_info中的editor页
        @param editor:待删除的editor
        @type editor:Pad_editor
        """
        self.remove_page(get_pos_in_list(self.__editor_list, editor) )
        self.__editor_list.remove(editor)
        self.show_all()

    def set_menubar(self, menubar):
        self.menubar = menubar

